package com.interview.a_多线程.e_Thread和Object中的重要方.h_join_原理;

/**
 * join 原理就是 wait（0）-- 等待唤醒
 * <p>
 * 因为 jvm代码中 Thread 机制就是
 * run执行完毕后 会 唤醒所有等待中线程
 * 所以join 中没有唤醒操作
 */
public class join {


    public static void main(String[] args) throws InterruptedException {
        Thread mainThread = Thread.currentThread();

        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(5000);
                    System.out.println(mainThread.getName() + " join线程中的状态  " + mainThread.getState());
                    System.out.println(Thread.currentThread().getName() + " 状态  " + Thread.currentThread().getState());
                    System.out.println(Thread.currentThread().getName() + " 执行完成\n");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "Join线程");

        thread.start();
        /**
         *  thread.join();  ==   synchronized (thread) {  thread.wait();  }
         */
        System.out.println("主线程开始等待新加入的线程操作完成！\n");
        System.out.println(mainThread.getName() + " join之前的状态 " + mainThread.getState());
//        thread.join();
        synchronized (thread) {
            thread.wait();
        }
        System.out.println("程序执行完成");
    }
}
